
** EARNINGS **

	// Income -- Aggregated Measures

		* Variables
			sum cash7dusd_e cashprev7dusd_e cash4wusd_e
			sum cash4wusd_b cash7dusd_b

		* 7d avg
			egen cashavg7d_e = rowmean (cash7dusd_e cashprev7dusd_e) if cash7dusd_e !=. & cashprev7dusd_e !=.
				la var cashavg7d_e "Inc: Cash 7d Avg (total) (USD)"

	// Income -- Disaggregated (Eco Activity table, sum across activities)


		* Baseline

			* for p1 and p2 we have cash 4w.
			egen cashsum4w_b = rowtotal (contract4wcashusd_b shop4wcashusd_b table4wcashusd_b buysell4wcashusd_b pettytrade4wcashusd_b trade4wcashusd_b office4wcashusd_b carload4wcashusd_b carwash4wcashusd_b moto4wcashusd_b carryload4wcashusd_b guard4wcashusd_b houseclswp4wcashusd_b scavenge4wcashusd_b beg4wcashusd_b farm4wcashusd_b drugsell4wcashusd_b stealing4wcashusd_b gambling4wcashusd_b smallbiz4wcashusd_b constr4wcashusd_b fish4wcashusd_b goldrubber4wcashusd_b other4wcashusd_b), missing
			replace cashsum4w_b = 0 if cashsum4w_b ==.
				la var cashsum4w_b "Cash 4w (sum from EA Table) (USD)"

			* for p3, we have profit 4w
			egen cashprofit4w_b = rowtotal (shop4wprofusd_b table4wprofusd_b buysell4wprofusd_b pettytrade4wprofusd_b trade4wprofusd_b office4wprofusd_b carload4wprofusd_b carwash4wprofusd_b moto4wprofusd_b carryload4wprofusd_b guard4wprofusd_b houseclswp4wprofusd_b scavenge4wprofusd_b beg4wprofusd_b farm4wprofusd_b drugsell4wprofusd_b stealing4wprofusd_b contract4wprofusd_b gambling4wprofusd_b smallbiz4wprofusd_b constr4wprofusd_b fish4wprofusd_b goldrubber4wprofusd_b), mi
			replace cashprofit4w_b = 0 if cashprofit4w_b ==.
				la var cashprofit4w_b "Profit 4w (sum from EA Table) (USD)"

			* create merged variable for baseline control
			gen cashsumprofit4w_b = cashsum4w_b
				replace cashsumprofit4w_b = cashprofit4w_b if p3 == 1
				la var cashsumprofit4w_b "Combination of cashsum4w_b (for P1 and P2) and cashprofit4w_b (for P3) (USD)"

		* Endline

			egen cashsum4w_e = rowtotal (contract4wcashusd_e shop4wcashusd_e table4wcashusd_e buysell4wcashusd_e pettytrade4wcashusd_e trade4wcashusd_e office4wcashusd_e carload4wcashusd_e carwash4wcashusd_e moto4wcashusd_e carryload4wcashusd_e guard4wcashusd_e houseclswp4wcashusd_e scavenge4wcashusd_e beg4wcashusd_e farm4wcashusd_e drugsell4wcashusd_e  stealing4wcashusd_e gambling4wcashusd_e smallbiz4wcashusd_e constr4wcashusd_e fish4wcashusd_e goldrubber4wcashusd_e other4wcashusd_e   ), missing
			replace cashsum4w_e = 0 if cashsum4w_e ==. & p1_e3wks==1
				la var cashsum4w_e "Cash 4w (sum from EA Table) (USD)"

			egen cashsum7d_e = rowtotal ( contract7dcashusd_e  stealing7dcashusd_e gambling7dcashusd_e smallbiz7dcashusd_e constr7dcashusd_e fish7dcashusd_e goldrubber7dcashusd_e other7dcashusd_e   drugsell7dcashusd_e farm7dcashusd_e beg7dcashusd_e scavenge7dcashusd_e houseclswp7dcashusd_e guard7dcashusd_e carryload7dcashusd_e moto7dcashusd_e carwash7dcashusd_e carload7dcashusd_e office7dcashusd_e trade7dcashusd_e pettytrade7dcashusd_e buysell7dcashusd_e table7dcashusd_e shop7dcashusd_e ), missing
			replace cashsum7d_e = 0 if cashsum7d_e ==. & (p1_e3wks==1 | p1_e5m==1 |  p2_e2wks ==1 | p2_e2w2w ==1| p1_e7m==1 | p1_e12m==1  | p1_e13m==1 | p3_e2w==1 | p3_e5w==1 | p2_e12m ==1 | p2_e13m ==1 | p3_e12m ==1 | p3_e13m==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1 | p1_e10y==1 | p2_e10y==1 | p3_e10y==1)
				la var cashsum7d_e "Cash 7d (sum from EA Table) (USD)"

			egen cashsumprev7d_e = rowtotal (contractprev7dcashusd_e shopprev7dcashusd_e tableprev7dcashusd_e buysellprev7dcashusd_e pettytradeprev7dcashusd_e tradeprev7dcashusd_e officeprev7dcashusd_e carloadprev7dcashusd_e carwashprev7dcashusd_e motoprev7dcashusd_e carryloadprev7dcashusd_e guardprev7dcashusd_e houseclswpprev7dcashusd_e scavengeprev7dcashusd_e begprev7dcashusd_e farmprev7dcashusd_e drugsellprev7dcashusd_e   stealingprev7dcashusd_e gamblingprev7dcashusd_e smallbizprev7dcashusd_e constrprev7dcashusd_e fishprev7dcashusd_e goldrubberprev7dcashusd_e otherprev7dcashusd_e  ), missing
			replace cashsumprev7d_e = 0 if cashsumprev7d_e==. & (p1_e5m==1 | p2_e2wks ==1 | p2_e2w2w ==1 | p1_e7m==1 | p1_e12m==1 | p1_e13m==1 | p3_e2w==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 | p3_e12m==1 | p3_e13m==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1 | p1_e10y==1 | p2_e10y==1 | p3_e10y==1)
				la var cashsumprev7d_e "Cash prev 7d (sum from EA Table) (USD)"

			egen profitsum7d_e = rowtotal (contract7dprofusd_e  stealing7dprofusd_e gambling7dprofusd_e smallbiz7dprofusd_e constr7dprofusd_e fish7dprofusd_e goldrubber7dprofusd_e other7dprofusd_e   drugsell7dprofusd_e farm7dprofusd_e beg7dprofusd_e scavenge7dprofusd_e houseclswp7dprofusd_e guard7dprofusd_e carryload7dprofusd_e moto7dprofusd_e carwash7dprofusd_e carload7dprofusd_e office7dprofusd_e trade7dprofusd_e pettytrade7dprofusd_e buysell7dprofusd_e table7dprofusd_e shop7dprofusd_e ), missing
			replace profitsum7d_e = 0 if profitsum7d_e==. & (p1_e3wks==1 | p1_e5m==1 | p2_e2wks ==1 | p2_e2w2w ==1 | p1_e7m==1 | p1_e12m==1 | p1_e13m==1| p3_e2w==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 | p3_e12m==1 | p3_e13m==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1 | p1_e10y==1 | p2_e10y==1 | p3_e10y==1)
				la var profitsum7d_e "Profit 7d (sum from EA Table) (USD)"

			egen profitsumprev7d_e = rowtotal (contractprev7dprofusd_e shopprev7dprofusd_e tableprev7dprofusd_e buysellprev7dprofusd_e pettytradeprev7dprofusd_e tradeprev7dprofusd_e officeprev7dprofusd_e carloadprev7dprofusd_e carwashprev7dprofusd_e motoprev7dprofusd_e carryloadprev7dprofusd_e guardprev7dprofusd_e houseclswpprev7dprofusd_e scavengeprev7dprofusd_e begprev7dprofusd_e farmprev7dprofusd_e drugsellprev7dprofusd_e   stealingprev7dprofusd_e gamblingprev7dprofusd_e smallbizprev7dprofusd_e constrprev7dprofusd_e fishprev7dprofusd_e goldrubberprev7dprofusd_e otherprev7dprofusd_e  ), missing
			replace profitsumprev7d_e = 0 if profitsumprev7d_e==. & (p1_e5m==1 | p2_e2wks ==1 | p2_e2w2w ==1 | p1_e7m==1 | p1_e12m==1 | p1_e13m==1| p3_e2w==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 | p3_e12m==1 | p3_e13m==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1 | p1_e10y==1 | p2_e10y==1 | p3_e10y==1)
				la var profitsumprev7d_e "Profit prev 7d (sum from EA Table) (USD)"

			egen cashsumavg7d_e = rowmean (cashsumprev7d_e cashsum7d_e) if (p1_e5m==1 | p2_e2wks ==1 | p2_e2w2w ==1 | p1_e7m==1| p1_e12m==1 | p1_e13m==1| p3_e2w==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 | p3_e12m==1 | p3_e13m==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1 | p1_e10y==1 | p2_e10y==1 | p3_e10y==1)
				la var cashsumavg7d_e "Cash 7d Avg (sum from EA Table) (USD)"

			egen profitsumavg7d_e = rowmean (profitsumprev7d_e profitsum7d_e) if (p1_e5m==1 | p2_e2wks ==1 | p2_e2w2w ==1 | p1_e7m==1 | p1_e12m==1 | p1_e13m==1| p3_e2w==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 | p3_e12m==1 | p3_e13m==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1 | p1_e10y==1 | p2_e10y==1 | p3_e10y==1)
				replace profitsumavg7d_e = profitsum7d_e if p1_e3wks
				la var profitsumavg7d_e "Profit 7d Avg (sum from EA Table) (USD)"

	// PROFITABILITY

		gen hourlywage7d_e = profitsum7d_e / hour7d_e
			la var hourlywage7d_e  "Hourly Wage"

	// LAGGED VARIABLES

		gen houravg7d_b = hour4w_b/4
		gen hour7d_b = hour4w_b/4

		gen illicit7dhrstot_b = illicit4whrstot_b/4
		gen formal7dhrstot_b = formal4whrstot_b/4
		gen informal7dhrstot_b = informal4whrstot_b/4

		gen illicit7dhrsavg_b = illicit4whrsavg_b/4
		gen formal7dhrsavg_b = formal4whrsavg_b/4
		gen informal7dhrsavg_b = informal4whrsavg_b/4

		gen profitsumavg7d_b = cashsumprofit4w_b/4
		gen profitsumprev7d_b = cashsumprofit4w_b/4
		gen profitsum7d_b = cashsumprofit4w_b/4

		/* Cap hours */
		foreach x in houravg7d_b hour7d_b illicit7dhrstot_b formal7dhrstot_b informal7dhrstot_b profitsumavg7d_b profitsumprev7d_b profitsum7d_b {
			qui sum `x', d
			qui replace `x' = r(p99) if `x'>r(p99) & !missing(`x')
		}
		/* Some weekly totals are above 24 hours a day, switch to 14 hours a day */
		qui replace houravg7d_b = 14*7 if houravg7d_b>14*7 & !missing(houravg7d_b)

	// CAP PROFITS

		foreach x in  b {
			qui sum cashprofit4w_`x', d
			gen cashprofit4wp99_`x' = cond(cashprofit4w_`x'>r(p99), r(p99), cashprofit4w_`x') if cashprofit4w_`x'!=.

		}
		qui sum cashsumprofit4w_b if round == 1, d
			gen cashsumprofit4wp99_b = cond(cashsumprofit4w_b>r(p99),r(p99),cashsumprofit4w_b) if !missing(cashsumprofit4w_b)
			la var cashsumprofit4wp99_b "Combination of cashsum4w_b (for P1 and P2) and cashprofit4w_b (for P3) (USD)"
		foreach x in profitsum  {
			qui sum `x'7d_e, d
			gen `x'p997d_e = cond(`x'7d_e>r(p99), r(p99), `x'7d_e)  if `x'7d_e!=.

			qui sum `x'prev7d_e, d
			gen `x'p99prev7d_e = cond(`x'prev7d_e>r(p99), r(p99), `x'prev7d_e)  if `x'prev7d_e!=.

			qui sum `x'7d_b, d
			gen `x'p997d_b = cond(`x'7d_b>r(p99), r(p99), `x'7d_b)  if `x'7d_b!=.

			qui sum `x'prev7d_b, d
			gen `x'p99prev7d_b = cond(`x'prev7d_b>r(p99), r(p99), `x'prev7d_b)  if `x'prev7d_b!=.
		}

		egen profitsump99avg7d_e = rowmean ( profitsump997d_e profitsump99prev7d_e) if (p1_e7m==1 | p1_e5m==1 | p2_e2wks ==1 | p2_e2w2w ==1 | p1_e12m==1 | p1_e13m==1| p3_e2w==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 | p3_e12m==1 | p3_e13m==1  | p1_e9y==1 | p2_e9y==1 | p3_e9y==1 | p1_e10y==1 | p2_e10y==1 | p3_e10y==1)
		egen profitsump99avg7d_b = rowmean ( profitsump997d_b profitsump99prev7d_b)
      			la var profitsump99avg7d_b "Baseline profit 7d Avg (sum from EA Table) (USD) (capped 99th)"


		drop profitsump99avg7d_e profitsump997d_e  profitsump99prev7d_e

		foreach x in profitsum  {
			qui sum `x'7d_e, d
			gen `x'p997d_e = cond(`x'7d_e>r(p99), r(p99), `x'7d_e)  if `x'7d_e!=.

			qui sum `x'prev7d_e, d
			gen `x'p99prev7d_e = cond(`x'prev7d_e>r(p99), r(p99), `x'prev7d_e)  if `x'prev7d_e!=.

		}

		egen profitsump99avg7d_e = rowmean ( profitsump997d_e profitsump99prev7d_e) if (p1_e13m==1 | p1_e12m ==1 | p1_e7m==1 | p1_e5m==1 | p2_e2wks ==1 | p2_e2w2w ==1 | p1_e3wks==1| p3_e2w==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 | p3_e12m==1 | p3_e13m==1 | p1_e10y==1 | p2_e10y==1 | p3_e10y==1 | p1_e9y==1 | p2_e9y==1 | p3_e9y==1 )
			la var profitsump99avg7d_e "Profit 7d Avg (sum from EA Table) (USD) (capped 99th)"

/*
 p1_e9y==1 | p2_e9y==1 | p3_e9y==1 | 18


    p1_e3wks==1|  16

common

p1_e7m==1 | p1_e5m==1 | p1_e12m==1 |   p1_e13m==1 | p2_e2wks ==1 |
p2_e2w2w ==1 | p3_e2w==1 | p3_e5w==1 | p2_e12m==1 | p2_e13m==1 |
 p3_e12m==1 | p3_e13m==1  | p1_e10y==1 | p2_e10y==1 | p3_e10y==1

 */
